<?php


namespace app\api\controller;

use app\api\model\DistributionConfig as Config;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\facade\Request;

class DistributionConfig extends Common
{
    /**
     * @param Request $request
     * @param Config $config
     * @return \think\response\Json
     */
    public function store(Request $request, Config $config)
    {
        $res = $config->save($request::only($config->getTableFields()));
        return $this->returnJson(200, 'ok', $res);
    }

    /**
     * @param $id
     * @param Config $config
     * @return \think\response\Json
     */
    public function show($id, Config $config)
    {
        try {
            return $this->returnJson(200,'ok', $config->findOrFail($id));
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    public function update($id, Config $config, Request $request)
    {
        try {
            $res = $config->findOrFail($id)->save($request::only($config->getTableFields()));
            return $this->returnJson(200,'ok', $res);
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

    /**
     * @param Config $config
     * @param Request $request
     * @return \think\response\Json
     * @throws DbException
     */
    public function index(Config $config, Request $request)
    {
        $res = $config->paginatePassPage($request::param('page',1), $request::param('size', 10));
        return $this->returnJson(200, 'ok', $res);
    }

    /**
     * @param $id
     * @param Config $config
     * @return \think\response\Json
     */
    public function delete($id, Config $config)
    {
        try {
            return $this->returnJson(200, 'ok', $config->findOrFail($id)->delete());
        } catch (DataNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        } catch (\Exception $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }

//    public function storeQualification($uid, Config $config)
//    {
//        return $this->returnJson(200, 'ok', $config::find()->);
//    }
}